$NG.AllReady(function (
	page,
	{
		useValuesChange,
		useClick,
		useUpdateRow,
		useUpdateRows,
		useDataIndexChange,
	}
) {
	var mstform = $NG.getCmpApi("p_form_impl_cost_budget_chg");
	var dgrid1 = $NG.getCmpApi("p_form_impl_labor_chg"); //实施人工p_form_impl_labor_chg
	var dgrid2 = $NG.getCmpApi("p_form_impl_daliy_chg"); //日常费用p_form_impl_daliy_chg
	var dgrid3 = $NG.getCmpApi("p_form_impl_sec_dev_chg"); //二次开发费用p_form_impl_sec_dev_chg

	{
		//初始化逻辑BEGIN
		if (mstform) {
			cntMFilter();
			getPtInfoByPhid();
		} else {
			console.error("mstform is not available");
		}
	} //初始化逻辑END

	{
		//表头字段变化逻辑BEGIN
		useValuesChange(({ args }) => {
			cntMFilter();
			getPtInfoByPhid();
			$NG.updateState((updater) => {
				updater.data.p_form_impl_cost_budget_chg.setProps({
					u_pc_no: args[0].phid_pc.PhId,
					u_cnt: {label:"",value:""},
					u_cnt_no:""
				});
			});
		}, "p_form_impl_cost_budget_chg.phid_pc");
		useValuesChange(({ args }) => {
			getCntMInfoByPhid();
		}, "p_form_impl_cost_budget_chg.u_cnt");
	} //表头字段变化逻辑END

	{
		//表体字段汇总逻辑BEGIN
		useUpdateRows(({ args }) => {
			caculatedgrid();
		}, "p_form_impl_labor_chg");

		useUpdateRows(({ args }) => {
			caculatedgrid();
		}, "p_form_impl_daliy_chg");

		useUpdateRows(({ args }) => {
			caculatedgrid();
		}, "p_form_impl_sec_dev_chg");
	} //表体字段汇总逻辑END

	function getPtInfoByPhid() {
		if (mstform.getItem("phid_pc").getValue()) {
			$NG.execServer(
				"getPtInfoByPhid",
				{ phid: mstform.getItem("phid_pc").getValue() },
				function (res) {
					const datas = JSON.parse(res.data)[0].extendObjects;
					//帮助字段赋值
					$NG.updateState((updater) => {
						updater.data.p_form_impl_cost_budget_chg.setProps({
							u_agreed_sts_dt: datas.start_date, //计划完工日期
							u_agreed_pcl_dt: datas.end_date, //计划竣工日期
							u_sqgw: {
								//售前顾问
								value: datas.user_sqgw_phid,
								label: datas.user_sqgw_name,
							},
							u_region: {
								//事业部or组织
								value: datas.phid_org_phid,
								label: datas.phid_org_name,
							},
							u_regional_center: {
								//区域中心
								value: datas.regional_center_phid,
								label: datas.regional_center_name,
							},
							u_product_line: {
								//产品线
								value: datas.midwife_phid,
								label: datas.midwife_name,
							},
							u_dept: {
								//部门
								value: datas.dept_phid,
								label: datas.dept_name,
							},
							                        u_pc_type: {//项目类型
                            value: datas.type_phid,
                            label: datas.type_name,
                        },
						});
					});
				}
			);
		}
	}

	function cntMFilter() {
		$NG.updateUI((updater, state) => {
			updater.fieldSetForm.p_form_impl_cost_budget_chg.u_cnt.setProps({
				onBeforeOpen: async () => {
					var phid_pc = mstform.getItem("phid_pc").getValue();
					if (!phid_pc) {
						$NG.alert("请先选择项目");
						return false;
					} else {
						return true;
					}
				},
				...(!!mstform.getItem("phid_pc").getValue() && {
					clientSqlFilter: {
						bill_type: 1,
						phid_pc: mstform.getItem("phid_pc").getValue(),
					},
				}),
			});
		});
	}

	function getCntMInfoByPhid(params) {
		$NG.execServer(
			"getCntMInfoByPhid",
			{ phid: mstform.getItem("u_cnt").getValue() },
			function (res) {
				const datas = JSON.parse(res.data)[0].extendObjects;
				//帮助字段赋值
				$NG.updateState((updater) => {
					//【注意这里的表名不能为控件ID，必须要是表名】
					updater.data.p_form_impl_cost_budget_chg.setProps({
						u_cnt_no: datas.pcm_no, //合同编码
						u_cnt_amt_vat: datas.cnt_sum_vat_fc, //合同价税合计
						u_invoice_type: {
							//发票类型
							value: datas.invoice_type_phid,
							label: datas.invoice_type_name,
						},
					});
				});
			}
		);
	}

	function caculatedgrid() {
		let u_total_impl_labor_amt = 0; //实施人工费
		let u_total_daily_amt = 0; //合计日常费用
		let u_total_sec_dev_amt = 0; //合计二次开发费用
		let u_total_impl_amt = 0; //合计总费用

		const Rows1 = dgrid1.getRows(); //设备清单页签
		const Rows2 = dgrid2.getRows(); //实施费页签
		const Rows3 = dgrid3.getRows(); //服务费页签
		Rows1.forEach((items) => {
			u_total_impl_labor_amt += parseFloat(items.u_labor_amt) || 0; //合同现额
		});
		Rows2.forEach((items) => {
			u_total_daily_amt += parseFloat(items.u_annual_impl_amt);
		});
		Rows3.forEach((items) => {
			u_total_sec_dev_amt += parseFloat(items.u_sec_dev_amt);
		});

		u_total_impl_amt =
			u_total_impl_labor_amt + u_total_daily_amt + u_total_sec_dev_amt;

		$NG.updateState((updater) => {
			updater.data.p_form_impl_cost_budget_chg.setProps({
				u_total_impl_labor_amt: u_total_impl_labor_amt, //
				u_total_daily_amt: u_total_daily_amt, //
				u_total_sec_dev_amt: u_total_sec_dev_amt, //
				u_total_impl_amt: u_total_impl_amt, //
			});
		});
	}
    
    // 添加列
    useValuesChange(({ args }) => {
        console.log(args[0]);
        dgrid1.clearRows();
        dgrid2.clearRows();
        dgrid3.clearRows();
        setTimeout(()=>{
			// 赋值
			function _sum (num) {
				$NG.updateState((updater)=>{
					updater.data.p_form_impl_cost_budget_chg.setProps({
						u_total_impl_amt_ori:num
					})
				})
			}
			// 临时存储上一次的费用
			const _countMaps = {
				_d1:0,
				_d2:0,
				_d3:0,
				set d1(value){
					this._d1 = value
					_sum(this._d1+this._d2+this._d3)
				},
				set d2(value){
					this._d2 = value
					_sum(this._d1+this._d2+this._d3)
				},
				set d3(value){
					this._d3 = value
					_sum(this._d1+this._d2+this._d3)
				},
			}
			Object.defineProperties(_countMaps,{
				_d1:{
				  configurable:false,
				  writable:true,
				  enumerable: false,
				  value:0
				},
				_d2:{
					configurable:false,
					writable:true,
					enumerable: false,
					value:0
				},
				_d3:{
					configurable:false,
					writable:true,
					enumerable: false,
					value:0
				},
			  })
			if (dgrid1) {
				$NG.execServer('getImplLaborBudgetInfo', {
					cnt: mstform.getItem('u_cnt').getValue()
				}, function (res) {
					let _data = null
					if(typeof res.data === 'string'){
						_data = JSON.parse(res.data)
					}else{
						_data = res.data
					}
					//默认增行,批量新增表体数据
					var gridArr = []; //定义表体数组
					var gridCount = res.count; //增加的行数,可根据从数据库从取出的数据总数增行
					for (var i = 0; i < gridCount; i++) {
						var gridObj = {
						};
						//对表体字段进行赋值,可以取数据库中的值进行赋值
					
						// 年度
						gridObj.u_year = _data[i].extendObjects.u_year;
						// 人员岗位
						gridObj.u_position = {label:_data[i].extendObjects.position_name,value:_data[i].extendObjects.position_phid}
						// 人员费率
						gridObj.u_personnel_rate = _data[i].extendObjects.u_personnel_rate;
						// 人工费合计
						gridObj.u_labor_amt = _data[i].extendObjects.u_labor_amt;
						// 数量
						gridObj.u_qty = _data[i].extendObjects.u_qty;
						// 1- 12 月
						for(let j = 1; j<=12; j++){
							const _str = `u_man_days_${j<10?'0'+j:j}`
							gridObj[_str] = _data[i].extendObjects[_str];
						}
						gridArr.push(gridObj);
					}
					//调用新增表体行数据的函数
					let _count = 0;
					gridArr.forEach(item => {
						_count += item.u_labor_amt
					})
					$NG.updateState((updater)=>{
						_countMaps.d1 = _count
						updater.data.p_form_impl_cost_budget_chg.setProps({
							u_total_impl_labor_amt_ori: _count,
						})
					})
					dgrid1.addRows(gridArr);
				})
			}
			if (dgrid2) {
				$NG.execServer('getImplDaliyBudgetInfo', {
					cnt: mstform.getItem('u_cnt').getValue()
				}, function (res) {
					let _data = null
					if(typeof res.data === 'string'){
						_data = JSON.parse(res.data)
					}else{
						_data = res.data
					}
					//默认增行,批量新增表体数据
					var gridArr = []; //定义表体数组
					var gridCount = res.count; //增加的行数,可根据从数据库从取出的数据总数增行
					for (var i = 0; i < gridCount; i++) {
						//对表体字段进行赋值,可以取数据库中的值进行赋值
						var gridObj = {
						};
						gridObj.u_year = _data[i].extendObjects.u_year;
						gridObj.u_annual_impl_amt = _data[i].extendObjects.u_annual_impl_amt;
						gridObj.u_expense_code = _data[i].extendObjects.u_expense_code;
						gridObj.u_expense_name = _data[i].extendObjects.u_expense_name;
						gridObj.u_expense_desc = _data[i].extendObjects.u_expense_description
						// 1- 12 月
						for(let j = 1; j<=12; j++){
							const _str = `u_expenses_${j<10?'0'+j:j}`
							gridObj[_str] = _data[i].extendObjects[_str];
						}
						gridArr.push(gridObj);
					}
					let _count = 0;
					gridArr.forEach(item => {
						_count += item.u_annual_impl_amt
					})
					$NG.updateState((updater)=>{
						_countMaps.d2 = _count
						updater.data.p_form_impl_cost_budget_chg.setProps({
							u_total_daily_amt_ori: _count
						})
					})
					//调用新增表体行数据的函数
					dgrid2.addRows(gridArr);
				})
			}
			if (dgrid3) {
				$NG.execServer('getImplSecDevBudgetInfo', {
					cnt: mstform.getItem('u_cnt').getValue()
				}, function (res) {
					let _data = null
					if(typeof res.data === 'string'){
						_data = JSON.parse(res.data)
					}else{
						_data = res.data
					}
					//默认增行,批量新增表体数据
					var gridArr = []; //定义表体数组
					var gridCount = res.count; //增加的行数,可根据从数据库从取出的数据总数增行
					for (var i = 0; i < gridCount; i++) {
						//对表体字段进行赋值,可以取数据库中的值进行赋值
						var gridObj = {
						};
						gridObj.u_sec_dev_sys = _data[i].extendObjects.u_sec_dev_sys
						gridObj.u_sec_dev_amt = _data[i].extendObjects.u_sec_dev_amt
						gridArr.push(gridObj);
					}
					let _count = 0;
					gridArr.forEach(item => {
						_count += item.u_sec_dev_amt
					})
					$NG.updateState((updater)=>{
						_countMaps.d3 = _count
						updater.data.p_form_impl_cost_budget_chg.setProps({
							u_total_sec_dev_amt_ori: _count
						})
					})
					//调用新增表体行数据的函数
					dgrid3.addRows(gridArr);
				})
			}
		},200)
    }, "p_form_impl_cost_budget_chg.u_cnt");
});
